<?php

namespace App\Models;

use App\Util\Tool;
use App\Util\Url;
use Illuminate\Database\Eloquent\Model;

class Footprint extends Model
{
    protected $table = "c_footprints";

    public function addFootPrint($data)
    {
        $date = date("Y-m-d", time());
        $dd = $this->getTodayLog($data, $date);
        $value = $this->where([['user_id', '=', $data['user_id']], ['date', '=', $date]])->orderByDesc("sort")->value("sort");
        if (empty($dd)) {
            $this->user_id = $data['user_id'];
            $this->goods_id = $data['goods_id'];
            $this->work_id = $data['work_id'];
            $this->type = $data['type'];
            $this->date = $date;
            $this->sort = $value + 1;
            return $this->save();
        } else {
            $dd->user_id = $data['user_id'];
            $dd->goods_id = $data['goods_id'];
            $dd->work_id = $data['work_id'];
            $dd->type = $data['type'];
            $dd->date = $date;
            $dd->sort = $value + 1;
            return $dd->save();
        }
    }

    public function getFootPrint($data)
    {
        $where = [];
        if (isset($data['type'])) {
            $where[] = ['type', '=', $data['type']];
        }
        $result = $this->where([['user_id', '=', $data['user_id']]])->where($where)->orderByDesc('sort')->simplePaginate(15);
        $course = new Course();
        $goods = new ProductGoods();
        if (!empty($result)) {
            $result = $result->toArray();
        }
        $host = new Url();
        foreach ($result['data'] as $key => $value) {
            if ($value['type'] == 1) { //课程
                $res = $course->where([['id', '=', $value['goods_id']]])->first();
                if(isset($res->course_img)){
                    $res->course_img = $host::getHttpHost().'upload/'. $res->course_img;
                }
            } elseif ($value['type'] == 2) { //商品
                $res = $goods->where([['id', '=', $value['goods_id']]])->first();
                if(isset($res->product_image)){
                    $res->product_image = $host::getHttpHost().'upload/'. $res->product_image;
                }
            } elseif ($value['type'] == 3) {

            }
            if (!empty($res)) {
                $res->work_id = $value['work_id'];
                $result['data'][$key]["goods"] = $res->toArray();
            } else {
                $result['data'][$key]["goods"] = null;
            }
        }
//        $result['data'] = self::array_group_by($result['data']);
        return $result;
    }

    public function getFootPrintIos($data)
    {
        $where = [];
        if (isset($data['type'])) {
            $where[] = ['type', '=', $data['type']];
        }
        $result = $this->where([['user_id', '=', $data['user_id']]])->where($where)->orderByDesc('sort')->simplePaginate(15);
        $course = new Course();
        $goods = new ProductGoods();
        if (!empty($result)) {
            $result = $result->toArray();
        }
        $host = new Url();
        foreach ($result['data'] as $key => $value) {
            if ($value['type'] == 1) { //课程
                $res = $course->where([['id', '=', $value['goods_id']]])->first();
                if (isset($res->course_img)) {
                    $res->course_img = $host::getHttpHost() . 'upload/' . $res->course_img;
                }
            } elseif ($value['type'] == 2) { //商品
                $res = $goods->where([['id', '=', $value['goods_id']]])->first();
                if (isset($res->product_image)) {
                    $res->product_image = $host::getHttpHost() . 'upload/' . $res->product_image;
                }
            } elseif ($value['type'] == 3) {

            }
            if (!empty($res)) {
                $res->work_id = $value['work_id'];
                $result['data'][$key]["goods"] = $res->toArray();
            } else {
                $result['data'][$key]["goods"] = [];
            }
        }
        $result['data'] = self::array_group_by($result['data']);
        return $result;
    }

    /**
     * @param $data
     * @param $date
     * @return mixed获取当天log
     */
    public function getTodayLog($data, $date)
    {
        $res = $this->where([['user_id', '=', $data['user_id']],
            ['goods_id', '=', $data['goods_id']],
            ['type', '=', $data['type']],
            ['date', '=', $date]])->first();
        return $res;
    }


    /***
     * @param $list
     * @return array
     * 分组排序
     */
    public static function array_group_by($list)
    {
        $newArray = array();
        $host = new Url();
        foreach ($list as $v) {
            $newArray[$v['date']][] = $v;
        }
        return $newArray;
    }
}
